/**
 * 所有爬虫的基类，
 * 自己写的spider都要继承该类
 */

const EventEmitter = require('events')

const Crawler = require('./crawler')
const Downloader = require('./downloader')
const Pipeline = require('./pipeline')
const {Request, FormRequest} = require('./request')
const Item = require('./item')


class Jcrapy {
  constructor() {
    this.downloader = new Downloader()
    this.pipeline = new Pipeline()
    this.eventEmitter = new EventEmitter()
    this.startTime = new Date().getTime()
  }

  /**
   * 爬虫启动时生成初始爬取的请求
   * @returns {Generator<*, void, ?>}
   */
  * startRequests() {
    throw new Error('startRequests() 方法必须被覆写，并且是个请求生成器')
  }


  /**
   * 解析下载器返回的 response 对象
   * @returns {Promise<void>}
   */
  * parse(response) {
    throw new Error('parse() 必须被覆写')
  }

  /**
   * 爬虫调用该方法启动
   */
  run() {
    const crawler = new Crawler()
    crawler.crawl(this)
  }

}


module.exports = {
  Jcrapy,
  Crawler,
  Downloader,
  Pipeline,
  Request,
  FormRequest,
  Item
}
